<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"
	contentType="text/html; charset=UTF-8"%>
<%@ page import="shopping.cart.*"%>

<%
	Cart c = (Cart) session.getAttribute("cart");
	if (c == null) {
		c = new Cart();
		session.setAttribute("cart", c);
	}
	double totalPrice = c.getTotalPrice();
	request.setCharacterEncoding("UTF-8");
	String action = request.getParameter("action");

	Map products = (HashMap) session.getAttribute("products");
	List<GoodsItem> items = c.getItems();

	if (action != null && action.trim().equals("add")) {
		String id = request.getParameter("id");
		Goods p = (Goods) products.get(id);
		GoodsItem gi = new GoodsItem();
		gi.setGoods(p);
		gi.setCount(1);
		c.add(gi);
	}

	if (action != null && action.trim().equals("clear")) {
		c.clear();
		
	}

	if (action != null && action.trim().equals("delete")) {
		String id = (String) request.getParameter("id");
		c.deleteItemById(id);
	}

	if (action != null && action.trim().equals("update")) {
		for (int i = 0; i < c.getItems().size(); i++) {
			GoodsItem ci = c.getItems().get(i);
			int count = Integer.parseInt(request.getParameter("p" + ci.getGoods().getId()));
			ci.setCount(count);
		}
	}
%>

<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>购物车</title>

</head>
<body>
	<H1>欢迎进入购物车</H1>

	<form action="Buy.jsp" method="get">
		<input type="hidden" name="action" value="update" />
		<table align="center" border="1" cellspacing="0">
			<tr>
				<td>商品ID</td>
				<td>商品名称</td>
				<td>购买数量</td>
				<td>单价</td>
				<td>总价</td>
				<td>操作</td>
			</tr>
			<%
				for (Iterator<GoodsItem> it = items.iterator(); it.hasNext();) {
					GoodsItem gi = it.next();
			%>
			<tr>
				<td><%=gi.getGoods().getId()%></td>
				<td><%=gi.getGoods().getName()%></td>
				<td>
					<%-- <input type="text" size=3
					name="<%="p" + ci.getProduct().getId()%>"
					value="<%=ci.getCount()%>"
					onkeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;"
					onchange="document.forms[0].submit()"></td> --%> <input
					onkeyup="value=value.replace(/[^/d]/g,'') "
					onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^/d]/g,''))"
					type="text" name="<%="p" + gi.getGoods().getId()%>"
					value="<%=gi.getCount()%>" onchange="document.forms[0].submit()" />
				</td>
				<td><%=gi.getGoods().getPrice()%></td>
				<td><%=gi.getGoods().getPrice() * gi.getCount()%></td>
				<td><a
					href="Buy.jsp?action=delete&id=<%=gi.getGoods().getId()%>">删除</a></td>
			</tr>
			<%
				}
			%>


			<tr>
				<td colspan=3 align="right">所有商品总价格为：</td>
				<td colspan=3><%=c.getTotalPrice()%></td>
			</tr>
			<%
				double total = 0;
				if (c.getTotalPrice() != 0)
					total = c.getTotalPrice();
				session.setAttribute("total", total);
			%>

			<tr>
				<td align="right"><a href="ShowProducts.jsp" target="cart">返回</a></td>
				<td><a href="Buy.jsp?action=clear">清空</a></td>
				<td>
					<a href="javascript:document.forms[0].submit()&action=update">修改</a>
				</td>
				<td colspan=3 align="right"><a
					href="CheckPay.jsp?total=<%=total%>&action=clear">支付</a></td>
			</tr>
		</table>
	</form>

</body>
</html>
